package com.gmrz.uaf.protocol.v1.processor.exception;

import com.gmrz.uaf.common.IErrorCode;

public enum UAFErrorCode implements IErrorCode {

    OK(0, "OK", 1200),
    UNKNOWN(-1, "Unknown error code", 1500),

    DB_DATABASE_CONNECTION_ERROR(1, "Failed to connect to database.", 2000),
    DB_FACET_LOAD_FAILED(2, "Failed to load facet id from database", 2001),
    DB_POLICY_LOAD_FAILED(3, "Failed to load policy from database", 2002),
    DB_SYSTEM_CONFIG_LOAD_FAILED(4, "Failed to load system config from database", 2003),
    DB_ADD_DEVICE_FAILED(5, "Failed to insert device info into database", 2004),
    DB_QUERY_DEVICE_LIST_FAILED(6, "Failed to query device info from database", 2005),
    DB_DEL_DEVICE_FAILED(7, "Failed to delete device info from database", 2006),
    DB_DEL_AUTHENTICATOR_FAILED(8, "Failed to delete authenticator info from database", 2007),
    DB_QUERY_AUTHENTICATOR_FAILED(9, "Failed to query authenticator info from database", 2008),
    DB_INSERT_AUTHENTICATOR_FAILED(10, "Failed to insert authenticator info to database", 2009),
    DB_UPDATE_AUTHENTICATOR_FAILED(11, "Failed to update authenticator info to database", 2010),
    DB_QUERY_METADATA_FAILED(12, "Failed to query metadata info from database", 2011),
    DB_QUERY_WHITE_LIST_FAILED(16, "Failed to load white list", 2015),
    DB_INSERT_FIDO_LOG_FAILED(17, "Failed to insert fido log into database", 2016),
    DB_INSERT_PLUGIN_DATA_FAILED(18, "Failed to insert plugin data into database", 2017),
    DB_UPDATE_AUTHENTICATOR_UVS_FAILED(59, "Failed to update authenticator uvs to database", 2018),
    DB_INSERT_CERTIFICATE_FAILED(10, "Failed to insert certificate info to database", 2019),
    DB_QUERY_VENDOR_LIST_FAILED(68, "Failed to load device white list from database", 2020),
    DB_QUERY_EQUIPMENT_LIST_FAILED(69, "Failed to load device black list from database", 2021),
    DB_QUERY_DEVICEID_LIST_FAILED(70, "Failed to load deviceId black list from database", 2022),
    DB_QUERY_FAILED(71, "Failed to load config data from database", 2023),
    DB_QIERY_CERTIFICATE_FAILED(73, "Failed to query certificate info to database", 2024),
    DB_UPDATE_CERTIFICATE_FAILED(82, "Failed to update certificate info to database", 2025),
    DB_INSERT_CODE_FAILED(83, "Failed to insert code info to database", 2026),
    DB_UPDATE_CODE_FAILED(84, "Failed to update code info to database", 2027),
    DB_INSERT_CERT_UPDATE_RECORD_FAILED(87, "Failed to insert cert update record to database", 2028),


    PROTOCOL_INVALID_UAF_RESPONSE(17, "Invalid UAF Response.", 3000),
    PROTOCOL_HEADER_VALIDATION_FAILED(18, "Failed to validate Operation Header.", 3001),
    PROTOCOL_HEADER_OP_TYPE_INVALID(19, "Failed to validate the Operation Type in Header.", 3002),
    PROTOCOL_FCP_VALIDATION_FAILED(20, "Failed to validate the Final Challenge Params.", 3003),
    PROTOCOL_POLICY_VALIDATION_FAILED(21, "Failed to validate authenticator assertion against the server policy.", 3004),
    PROTOCOL_VERSION_NOT_SUPPORTED(22, "Protocol version is not supported.", 3005),
    PROTOCOL_SERVERDATA_VALIDATION_FAILED(23, "Failed to validate the ServerData.", 3006),
    PROTOCOL_APPID_VALIDATION_FAILED(24, "Failed to validate AppID.", 3007),
    PROTOCOL_CHALLENGE_VALIDATION_FAILED(25, "Failed to validate server challenge.", 3008),
    PROTOCOL_FACETID_VALIDATION_FAILED(26, "Failed to validate facetID.", 3009),
    PROTOCOL_TLSDATA_VALIDATION_FAILED(27, "Failed to validate TLS data.", 3010),
    PROTOCOL_ASSERTION_SCHEME_INVALID(28, "Failed to validate Assertion Scheme.", 3011),
    PROTOCOL_SIGNDATA_VALIDATION_FAILED(29, "Failed to validate authenticator signed data.", 3012),
    PROTOCOL_AAID_VALIDATION_FAILED(30, "Failed to validate AAID.", 3013),
    PROTOCOL_AUTHENTICATION_MODE_INVALID(31, "Failed to validate authentication mode.", 3014),
    PROTOCOL_INVALID_AUTH_ASSERTION(32, "Failed to parse authentication assertion TLV.", 3015),
    PROTOCOL_FCP_HASH_VALIDATION_FAILED(33, "Failed to validate the FCP hash in authenticator assertion.", 3016),
    PROTOCOL_ATTESTATION_TYPE_NOT_FOUND(34, "Failed to locate the attestation type in authenticator metadata.", 3017),
    PROTOCOL_KEY_FORMAT_NOT_FOUND(35, "Failed to locate the key format in authenticator metadata.", 3018),
    PROTOCOL_TXN_DISPLAY_INFO_NOT_FOUND(36, "Failed to locate the tcDisplayPNGCharacteristics for authenticator.", 3019),
    PROTOCOL_ALGORITHM_NOT_FOUND(37, "Failed to locate authentication algorithm in authenticator metadata.", 3020),
    PROTOCOL_INVALID_SIGN_COUNTER(38, "Invalid value of sign counter.", 3021),
    PROTOCOL_TXN_HASH_VALIDATION_FAILED(39, "Failed to validate transaction hash.", 3022),
    PROTOCOL_TLV_PARSING_FAILED(40, "Failed to parse TLV data.", 3023),
    PROTOCOL_TXN_CONTENT_CREATE_FAILED(41, "Failed to create the transaction content", 3024),
    PROTOCOL_ATTESTATION_CERT_VALIDATION_FAILED(42, "Failed to validate the attestation certificate.", 3025),
    PROTOCOL_ATTESTATION_SIGNATURE_VALIDATION_FAILED(43, "Failed to validate the attestation signature.", 3026),
    PROTOCOL_SIGNATURE_VALIDATION_FAILED(44, "Failed to validate the signature.", 3027),
    PROTOCOL_EXACT_MATCH_NOT_FOUND(45, "Failed to locate exact match for authenticator.", 3028),
    PROTOCOL_INVALID_REG_ASSERTION(46, "Failed to parse registration assertion TLV.", 3029),
    PROTOCOL_UVI_NOT_NONE(62, "uvi can not be empty", 3030),

    PROTOCOL_ADD_UVI_MAX_LENGTH(57, "add uvi exceed max num.", 3031),
    PROTOCOL_ADD_UVI_TIME_OUT(58, "add uvi exceed time out.", 3032),
    PROTOCOL_AUTH_TYPE_FAIL(60, "auth type error.", 3033),

    PROTOCOL_GENERATE_AUTHTOKEN(64, "generate authtoken error.", 3034),
    PROTOCOL_AUTHTOKEN_NOT_FOUND(65, "authtoken not found.", 3035),
    PROTOCOL_OOB_STATUS_ERROR(66, "get oob status error.", 3036),
    DEVICE_ID_IN_BLACKLIST(67, "The DeviceID is in the blacklist.", 3037),
    PROTOCOL_FAILED_TO_GET_CERT(74, "Failed to get cert num from redis", 3038),
    PROTOCOL_REG_CLOSED(79, "The registration interface is closed.", 3039),
    PROTOCOL_AUTH_CLOSED(80, "The authentication interface is closed.", 3040),
    PROTOCOL_REG_MAX_FAILED(81, "The number of registered devices is greater than the maximum.", 3041),
    PROTOCOL_AUTHTOKEN_STATUS_ERROR(84, "Authtoken status fail.", 3042),
    PROTOCOL_CUSTNO_ERROR(85, "Customer number inconsistency.", 3043),


    VALIDATE_CACERTIFICATE_FAILED(59, "Failed to validate the certificate.", 4000),
    GET_CACERTIFICATE_SIGNATUREDATA_FAILED(61, "Failed to obtain the certificate signatureData.", 4001),
    GET_CACERTIFICATE_SIGNATURE_FAILED(62, "Failed to obtain the certificate signature.", 4002),
    GET_CACERTIFICATE_PUBLICKEY_FAILED(63, "Failed to obtain the certificate publicKey.", 4003),
    CERT_COUNT_FAILED(72, "The number of certificates is greater than the maximum.", 4004),
    PROTOCOL_VALIDATE_P7_CONTEXT_HASH_FAILED(75, "Failed to validate the p7 transaction content hash.", 4005),
    PROTOCOL_VALIDATE_P10_DN_HASH_FAILED(76, "Failed to validate the p10 dn hash.", 4006),
    PROTOCOL_TXN_CONTENT_LENGTH_FAILED(77, "The transaction content is too long", 4007),
    PROTOCOL_TXN_CONTENT_SHOW_LENGTH_FAILED(78, "The transaction show content is too long", 4008),
    PROTOCOL_UNIQUE_REQUEST_FAILED(82, "There is only one registration for the same device at the same time.", 4009),
    PROTOCOL_VALIDATE_CERT_STATUS_FAILED(86, "The certificate status is not activated.", 4010),
    PROTOCOL_ACTIVITY_CERT_STATUS_FAILED(87, "Failed to activate the certificate status.", 4011),
    PROTOCOL_NOT_FOUND_CERT_AUTHORIZATION_CODE_FAILED(87, "Failed to found cert authorization code.", 4012),
    PROTOCOL_FROZEN_CERT_STATUS_FAILED(86, "The certificate status is frozen.", 4013),
    PROTOCOL_VALIDATE_KRD_FAILED(91, "The Device public key in metadata is empty.", 4014),
    PROTOCOL_UPDATE_CERT_TIMEOUT_FAILED(92, "Update certificate timeout.", 4015),
    PROTOCOL_VALIDATE_DEVICEID_FAILED(93, "Failed to validate the deviceID.", 4016),
    PROTOCOL_VALIDATE_P7_CERTIFICATE_FAILED(94, "Failed to validate the p7 certificate.", 4017),
    PROTOCOL_VALIDATE_P7_ES_FLAG_FAILED(95, "Failed to validate the es flag.", 4018),
    PROTOCOL_VALIDATE_P7_ES_HASH_FAILED(96, "Failed to validate the es hash.", 4019),
    PROTOCOL_VALIDATE_P7_ES_HASH_EMPTY(97, "The es hash is empty.", 4020),

    ERROR_APPSECRET_LICENSE_NOT_FOUND(88, "There is no interface access restricted node in the request", 5000),
    ERROR_APPSECRET_REQUEST_NOT_COMPLETE(89, "The requested data was tampered with", 5001),
    ERROR_APPSECRET_CONTEXT_NOT_FOUND(90, "There is no [context] node in the request", 5002),


    PARAM_VALIDATE_FAILED(47, "Failed to validate parameter", 1400),
    PARAM_INVALID_JSON(48, "Invalid JSON data", 1401),


    BIZ_POLICY_NOT_FOUND(49, "Requested policy is not available or policy do not have aaid on server.", 1402),
    BIZ_WHITE_LIST_FORBIDDEN(50, "The access is denied by FIDO Server", 1403),
    BIZ_AUTHENTICATOR_NOT_FOUND(51, "Failed to locate a registered authenticator.", 1404),
    SYS_POLICY_VERIFICATION_FAILED(52, "Policy Verification Failed.", 1405),
    SYS_CACHE_FACET_FETCH_FAILED(53, "Failed to load facet ID from cache", 1406),
    SYS_POSSIBLE_ATTACK(54, "Repeat request,Possible replay attack", 1407),
    ERROR_DEVICE_NOT_FOUND(55, "Failed to locate registered device.", 1408),
    PROTOCOL_ADD_UVI(56, "Add uvi.", 1409),
    PROTOCOL_GO_ON_BUSINESS(59, "Registered voiceprint successful please continue the next one.", 1410),
    DEVICE_NOT_IN_WHITELIST(60, "The device not in whitelist.", 1411),
    DEVICE_IN_BLACKLIST(61, "The device in blacklist.", 1412),

    ERROR_KEYSTORE_CERTCHAIN_VALIDATION_FAILED(35, "Failed to Certificate chain validation is missing.", 1434);


    private int code;

    private int statusCode;

    private String message;

    UAFErrorCode(int code, String message, int statusCode) {
        this.code = code;
        this.message = message;
        this.statusCode = statusCode;
    }

    public int getCode() {
        return this.code;
    }

    public int getStatusCode() {
        return this.statusCode;
    }

    public String getMessage() {
        return this.message;
    }

    public String toString() {
        return "ErrorCode=[" + this.code + "], Message=[" + this.message + "]";
    }

    public static UAFErrorCode forErrorCode(int errorCode) {
        for (UAFErrorCode uafEc : values()) {
            if (uafEc.code == errorCode) {
                return uafEc;
            }
        }
        return UNKNOWN;
    }

}
